package com.cms.recursoshumanos.ui.menu_comedor;

import java.util.Calendar;

import com.cms.recursoshumanos.cache.ListaTipoMenuCache;
import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.TipoMenuListButtonField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.ListStyleButtonSetScrollManager;
import com.cms.recursoshumanos.container.SearchBarManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.ListaComidaMenuScreenHandler;
import com.cms.recursoshumanos.handler.ListaTipoMenuScreenHandler;
import com.cms.recursoshumanos.object.list.ListaTipoMenu;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;
import net.rim.device.api.ui.picker.DateTimePicker;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class ListaTipoMenuScreen extends RecursosHumanosScreen implements FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
	//static Utileria tools = new Utileria();
	private ListaTipoMenu listado_tipo_menu;
	
	public static ListaTipoMenuScreen instance = null;
	
	LabelField label_selector_tipo_evento;
	ForegroundManager _foreground;
	
	//SearchBarManager sbm;
	ListStyleButtonSetScrollManager buttonSet;
	VerticalFieldManager vfm;
	TwoColumnFieldManager twoColumnManager;
	
	public static ListaTipoMenuScreen getInstance(ListaTipoMenu listado_tipo_menu)
	{
		
		if (instance == null)
		{
			instance = new ListaTipoMenuScreen(listado_tipo_menu);
		}
		else
		{
			instance.listado_tipo_menu = listado_tipo_menu;
			instance.reset();
		}
		return instance;
	}
	public ListaTipoMenuScreen(ListaTipoMenu listado_tipo_menu) 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		this.listado_tipo_menu = listado_tipo_menu;
		setear_componenetes();
	}
	protected void makeMenu(Menu menu, int instance) 
	{
		MenuItem refreshButton = new MenuItem("Actualizar", 0, 0) 
        {
			public void run() 
			{
				ListaTipoMenuScreenHandler handler = new ListaTipoMenuScreenHandler();
	   			handler.refreshListadoTipoMenu(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
	   					UserInfoCache.getInstance().getUsuario().getPassword());
	   			
			}
		};
		menu.add(refreshButton);
		super.makeMenu(menu, instance);
	}
	public void reset() 
	{
		vfm.deleteAll();
		//loading = false;
		try 
		{
			if(buttonSet.getFieldCount() > 0) 
			{
				//sbm.setFocus();
				buttonSet.deleteAll();
			}
		} 
		catch (Exception e) 
		{
			//ShowDialogAlert alert = new ShowDialogAlert("Exception in delete fields...");
		}
		showListField();
		//pis = Json.getInstance().getPIs();
	}
	public void showListField()
	{
		buttonSet.deleteAll();
		TipoMenuListButtonField link;
		
		for(int i = 0; i < listado_tipo_menu.cantElementListaTipoMenu() ; i++) 
		{

			link = new TipoMenuListButtonField(listado_tipo_menu.getTipoMenuAtIndex(i), LabelField.ELLIPSIS) 
			{
			
				protected void paintBackground( Graphics g )
				{
					  COLOR_BACKGROUND_FOCUS = Constant.COLOR_LIST_FOCUS;
					  COLOR_BACKGROUND_FOCUS_ACTION = Constant.COLOR_FOCUS_NORMAL;

				      if( _drawPosition < 0 ) 
				      {
				          // it's like a list field, let the default background be drawn
				          super.paintBackground( g );
				          return;
				      }
				      
				      int oldColour = g.getColor();
				      
				      int focus = COLOR_BACKGROUND_FOCUS;
				      if(_doAction) 
				    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
				      
				      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
				      try 
				      {
				          if( _drawPosition == 0 ) 
				          {
				              // Top
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else if( _drawPosition == 1 ) 
				          {
				              // Bottom 
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				          } 
				          else if( _drawPosition == 2 ) 
				          {
				              // Middle
				              g.setColor( background );
				              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
				              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
				          } 
				          else 
				          {
				              // Single
				              g.setColor( background );
				              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				              g.setColor( COLOR_BORDER );
				              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
				          }
				      } 
				      finally 
				      {
				          g.setColor( oldColour );
				      }
				}
			};
			link.setChangeListener( this );
	        buttonSet.add( link );
		}
	}
	private void setear_componenetes() 
	{
		setTitle("Menu del Comedor");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE  );
		_foreground = new ForegroundManager(NO_VERTICAL_SCROLL);
		
		twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 6, 6, 6, 6 );
        twoColumnManager.setPadding( 2, 2, 2, 2 );
		
        buttonSet = new ListStyleButtonSetScrollManager();
        buttonSet.setScreen(this);
        
		//sbm = new SearchBarManager();
		LabelField lbl_mensaje = new LabelField("Seleccione a un Tipo de Menu", LabelField.HCENTER | FIELD_HCENTER);
		twoColumnManager.add ( lbl_mensaje );
		Field separator = new SeparatorField();
        separator.setMargin( 2, 0, 2, 0 );
        twoColumnManager.add( separator );
        
        //LabelField tipo_menu = new LabelField("Menu de "+this.elMenu.getTipoMenu().getNombreTipoMenu());
        //String fecha_seleccionada = tools.transformDateFormat(this.elMenu.getFechaMenu());
        
        showListField();
		twoColumnManager.add(buttonSet);
		
        //_foreground.add(sbm);
        _foreground.add( twoColumnManager );
        add( _foreground );
			
	}
	public void fieldChanged(Field field, int context) 
	{
		Calendar calSelecc = Calendar.getInstance();
		Calendar calHoy = Calendar.getInstance();
		
		calHoy.set(Calendar.HOUR_OF_DAY, 0);
		calHoy.set(Calendar.MINUTE, 0);
		calHoy.set(Calendar.MILLISECOND, 0);
		
		DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), Constant.DATE_FORMAT_NORMAL, null);
		
		datePicker.setMinimumDate(calHoy);
		
		if (datePicker.doModal())
		{
			
			calSelecc.setTime(datePicker.getDateTime().getTime());
			
			calSelecc.set(Calendar.HOUR_OF_DAY, 0);
			calSelecc.set(Calendar.MINUTE, 0);
			calSelecc.set(Calendar.MILLISECOND, 0);
			
			
			
			if (calSelecc.getTime().getTime() < calHoy.getTime().getTime())
			{
				ShowDialogAlert alerta = new ShowDialogAlert("la fecha no puede ser menor que la de hoy");
				alerta.run();
			}
			else
			{
				ListaComidaMenuScreenHandler handler = new ListaComidaMenuScreenHandler();
				handler.getListaComidaMenu(UserInfoCache.getInstance().getUsuario().getNombre_Usuario(), 
						UserInfoCache.getInstance().getUsuario().getPassword(),
												calSelecc.getTime(),
												ListaTipoMenuCache.getInstance().
													getListaTipoMenu().getTipoMenuAtIndex(field.getIndex()).getTipoMenuId());
			}
			
		}
		
		
	}
	
	
	
}

